package cn.lzd.mr.PracticeGroup;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 场景a文件b文件，找出b文件中name字段包含a文件的name字段的b文件内容
 */
public class GroupDemo2 {

    static class Mp extends Mapper<LongWritable, Text, PBean, PBean> {
        PBean bean = new PBean();
        String filename;

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            FileSplit fileSplit = (FileSplit) context.getInputSplit();
            filename = fileSplit.getPath().getName();
            System.out.println("====================hello=======================" + filename);
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String val = value.toString();
            bean.setName(val);
            bean.setFilename(filename);
            bean.setFullText(val);
            context.write(bean, bean);
        }
    }

    static class Rd extends Reducer<PBean, PBean, PBean, NullWritable> {
        @Override
        protected void reduce(PBean key, Iterable<PBean> values, Context context) throws IOException, InterruptedException {
//            values.forEach((x) -> {
//
//                try {
//                    context.write(x, NullWritable.get());
//                } catch (IOException e) {
//                    e.printStackTrace();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
            context.write(key,NullWritable.get());
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        String inpath = "/C:/Users/lzd/Desktop/name,/C:/Users/lzd/Desktop/fl";
        String outpath = "/G:/out1";

        Configuration configuration = new Configuration();
        Job job = Job.getInstance(configuration);

        job.setJarByClass(GroupDemo2.class);
//        job.setGroupingComparatorClass(PracticeCustomGroup2.class);

        job.setMapperClass(GroupDemo2.Mp.class);
        job.setReducerClass(GroupDemo2.Rd.class);

        job.setMapOutputKeyClass(PBean.class);
        job.setMapOutputValueClass(PBean.class);

        job.setOutputKeyClass(PBean.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.addInputPaths(job, inpath);
        FileOutputFormat.setOutputPath(job, new Path(outpath));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
